package serova.valeriya.electricity;

import java.util.ArrayList;
import java.util.List;

public class ModeFactory {
	
	private static ModeFactory modeFactory = null;
	
	public static ModeFactory getCurrentInstance() {
		if(null == modeFactory) {
			modeFactory = new ModeFactory();
		}
		
		return modeFactory;
	}
	
	private ModeFactory() {}

	public List<ApplicationMode> getAvailableModes() {
		List<ApplicationMode> result = new ArrayList<ApplicationMode>();
		for (ApplicationMode mode : ApplicationMode.values()) {
			if (mode.isReady()) {
				result.add(mode);
			}
		}
		
		checkNotEmpty(result);
		return result;
	}

	private void checkNotEmpty(List<ApplicationMode> result) {
		if (result.isEmpty()) {
			throw new IllegalStateException("At least one mode should be available");
		}
	}

	public Worker getWorker(ApplicationMode mode) {
		if (!getAvailableModes().contains(mode)) {
			return null;
		}
		return mode.getWorker();
	}
}
